//
//  UserPayingViewController.m
//  Assistant
//
//  Created by wyopenminded on 2016/11/12.
//  Copyright © 2016年 秦慕乔. All rights reserved.
//

#import "UserPayingViewController.h"

@interface UserPayingViewController ()<UIAlertViewDelegate>
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *payStatusLabel;
@property (strong, nonatomic) UIButton *leftBtn;
@property (strong, nonatomic) UIView *backgView;
@property (strong, nonatomic) UIImageView *payTypeImage;
@property (strong, nonatomic) UILabel *amountL;
@property (strong, nonatomic) NSTimer *timer;

@end

@implementation UserPayingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)setupNavView{
    
    [self.view addSubview:({
        _backgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
        _backgView.backgroundColor = [UIColor whiteColor];
        _backgView;
    })];
    
    [self.view addSubview:({
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont boldSystemFontOfSize:18];
        _titleLabel.textColor = [UIColor colorWithHex:0x333333];
        _titleLabel.text = @"扫一扫";
        _titleLabel;
    })];
    
    [self.view addSubview:({
        _leftBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_leftBtn setImage:[UIImage imageNamed:@"BlackNew_back"] forState:(UIControlStateNormal)];
        [_leftBtn addTarget:self action:@selector(returnBackToLastVC) forControlEvents:(UIControlEventTouchUpInside)];
        _leftBtn;
    })];
    
    __weak __typeof(&*self)superView = self;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(superView.view.mas_leading).with.offset((60*WIDTH)/320);
        make.centerY.mas_equalTo(_backgView.mas_centerY).with.offset(10.0f);
    }];
    
    [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_backgView.mas_centerY).with.offset(10.0f);
        make.leading.mas_equalTo(superView.view.mas_leading).with.offset(8.0f);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
}

#pragma mark- 直接扫描二维码
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects.count>0) {
        [self.session stopRunning];
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
        NSString *obtainString = [NSString stringWithFormat:@"%@", metadataObject.stringValue];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ScanSuccess" object:obtainString];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"扫描失败" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;{
    
    // the user clicked OK
    
    if (buttonIndex == 0)
        
    {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}


- (void)returnBackToLastVC{
    [_timer invalidate];
    _timer = nil;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


@end
